<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-8-14
 * Time: 下午3:00
 */
class template_model extends CI_Model{
   private $display_error = '';
   
    
   
   public function create()
   {
      $field_nums = $this->input->post('field_num');
      if (count($field_nums) != 64){
         $this->display_error = '必须上送64个域数据';
         return false;
      }
      $len_section_type = $this->input->post('len_section_type');
      if (count($len_section_type) != 64){
         $this->display_error = '必须上送64个域数据';
         return false;
      }
      $len_section_encode = $this->input->post('len_section_encode');
      if (count($len_section_encode) != 64){
         $this->display_error = '必须上送64个域数据';
         return false;
      }
      $data_section_len = $this->input->post('data_section_len');
      if (count($data_section_len) != 64){
         $this->display_error = '必须上送64个域数据';
         return false;
      }
      $data_section_encode = $this->input->post('data_section_encode');
      if (count($data_section_encode) != 64){
         $this->display_error = '必须上送64个域数据';
         return false;
      }
      $template = array_map(function ($fnum, $len_section_type, $len_section_encode, $data_section_len, $data_section_encode){
         return array(
             'field' => $fnum, 
             'type' => $len_section_type, 
             'len_encode' => $len_section_encode, 
             'data_len' => $data_section_len, 
             'data_encode' => $data_section_encode
         );
      }, $field_nums, $len_section_type, $len_section_encode, $data_section_len, $data_section_encode);
      $json = json_encode($template);
      
      $data = array(
            'uid' => $this->session->userdata('uid'),
            'customer' => $this->input->post('customer'),
            'name' => $this->input->post('name'),
            'content' => $json
      );
      
      $result = $this->db->insert('template', $data);
      if (!$result || $this->db->affected_rows() == 0){
         $this->display_error = '创建模板失败';
         return false;
      }
       $this->init_count();
      return true;
   }
   
   public function display_errors()
   {
      return $this->display_error;
   }
   
   public function get($offset = '', $value= 0)
   {
       
       $user = $this->session->userdata('uid');
       
       $result = $this->db->select('id, customer, name')->
                from('template')->
                where(array('uid' => $user))->
                limit($value, $offset)->
                get()->result_array();
       if (!$result){
           return false;
       }
       return $result;
       
       
   }
    public function get_by_id($id = '')
    {
        if ($id == ''){
            return false;
        }
        
        $user = $this->session->userdata('uid');
        
        $result = $this->db->select('*')->from('template')->
                    where(array('id' => $id, 'uid' => $user))->
                    get()->
                    result_array();
        if (!$result){
            return false;
        }
        return $result;
    }
    public function init_count()
    {
        $result = $this->db->select('count(id) as numbers')->
        from('template')->
        where(array('uid' => $this->session->userdata('uid')))->
        get()->
        result_array();
        if ($result && count($result) > 0){
            $this->session->set_userdata('template_numbers', $result[0]['numbers']);
        }
    }
    
    
    public function update()
    {
        $field_nums = $this->input->post('field_num');
        if (count($field_nums) != 64){
            $this->display_error = '必须上送64个域数据';
            return false;
        }
        $len_section_type = $this->input->post('len_section_type');
        if (count($len_section_type) != 64){
            $this->display_error = '必须上送64个域数据';
            return false;
        }
        $len_section_encode = $this->input->post('len_section_encode');
        if (count($len_section_encode) != 64){
            $this->display_error = '必须上送64个域数据';
            return false;
        }
        $data_section_len = $this->input->post('data_section_len');
        if (count($data_section_len) != 64){
            $this->display_error = '必须上送64个域数据';
            return false;
        }
        $data_section_encode = $this->input->post('data_section_encode');
        if (count($data_section_encode) != 64){
            $this->display_error = '必须上送64个域数据';
            return false;
        }
        $template = array_map(function ($fnum, $len_section_type, $len_section_encode, $data_section_len, $data_section_encode){
            return array(
                'field' => $fnum,
                'type' => $len_section_type,
                'len_encode' => $len_section_encode,
                'data_len' => $data_section_len,
                'data_encode' => $data_section_encode
            );
        }, $field_nums, $len_section_type, $len_section_encode, $data_section_len, $data_section_encode);
        $json = json_encode($template);

        $data = array(
            'customer' => $this->input->post('customer'),
            'name' => $this->input->post('name'),
            'content' => $json
        );
        
        $where = array(
            'uid' => $this->session->userdata('uid'),
            'id' => $this->input->post('id')
        );

        $result = $this->db->update('template', $data, $where);
        if (!$result || $this->db->affected_rows() == 0){
            $this->display_error = '更新模板失败';
            return false;
        }
        return true;
    }
    
    public function delete($id = '')
    {
        if ($id == ''){
            return false;
        }
        
        $result = $this->db->delete('template', array('id' => $id));
        if (!$result || $this->db->affected_rows() == 0){
            return false;
        }
        $this->init_count();
        return true;
    }


    /**
     * 标准银联规范模板
     * @return array
     * 
     */
    public function get_cup_template()
    {
       return array(
           array(
               'field' => '1',
               'use' => false
           ),
           array(
               'field' => '2',
               'use' => true,
               'len_section_type' => 'llvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 19,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '3',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 6,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '4',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 12,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '5',
               'use' => false
           ),
           array(
               'field' => '6',
               'use' => false
           ),
           array(
               'field' => '7',
               'use' => false
           ),
           array(
               'field' => '8',
               'use' => false
           ),
           array(
               'field' => '9',
               'use' => false
           ),
           array(
               'field' => '10',
               'use' => false
           ),
           array(
               'field' => '11',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 6,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '12',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 6,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '13',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 4,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '14',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 4,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '15',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 4,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '16',
               'use' => false
           ),
           array(
               'field' => '17',
               'use' => false
           ),
           array(
               'field' => '18',
               'use' => false
           ),
           array(
               'field' => '19',
               'use' => false
           ),
           array(
               'field' => '20',
               'use' => false
           ),
           array(
               'field' => '21',
               'use' => false
           ),
           array(
               'field' => '22',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 3,
               'data_section_encode' => 'bcd_right'
           ),
           array(
               'field' => '23',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 3,
               'data_section_encode' => 'bcd_right'
           ),
           array(
               'field' => '24',
               'use' => false
           ),
           array(
               'field' => '25',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 2,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '26',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 2,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '27',
               'use' => false
           ),
           array(
               'field' => '28',
               'use' => false
           ),
           array(
               'field' => '29',
               'use' => false
           ),
           array(
               'field' => '30',
               'use' => false
           ),
           array(
               'field' => '31',
               'use' => false
           ),
           array(
               'field' => '32',
               'use' => true,
               'len_section_type' => 'llvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 11,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '33',
               'use' => false
           ),
           array(
               'field' => '34',
               'use' => false
           ),
           array(
               'field' => '35',
               'use' => true,
               'len_section_type' => 'llvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 37,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '36',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 104,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '37',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 12,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '38',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 6,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '39',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 2,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '40',
               'use' => false
           ),
           array(
               'field' => '41',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 8,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '42',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 15,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '43',
               'use' => false
           ),
           array(
               'field' => '44',
               'use' => true,
               'len_section_type' => 'llvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 25,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '45',
               'use' => false
           ),
           array(
               'field' => '46',
               'use' => false
           ),
           array(
               'field' => '47',
               'use' => false
           ),
           array(
               'field' => '48',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 322,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '49',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 3,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '50',
               'use' => false
           ),
           array(
               'field' => '51',
               'use' => false
           ),
           array(
               'field' => '52',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 8,
               'data_section_encode' => 'bin'
           ),
           array(
               'field' => '53',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 16,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '54',
               'use' => true,
               'len_section_type' => 'llvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 20,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '55',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 255,
               'data_section_encode' => 'bin'
           ),
           array(
               'field' => '56',
               'use' => false
           ),
           array(
               'field' => '57',
               'use' => false
           ),
           array(
               'field' => '58',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 100,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '59',
               'use' => false
           ),
           array(
               'field' => '60',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 17,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '61',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 29,
               'data_section_encode' => 'bcd_left'
           ),
           array(
               'field' => '62',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 512,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '63',
               'use' => true,
               'len_section_type' => 'lllvar',
               'len_section_encode' => 'bcd_right',
               'data_section_len' => 163,
               'data_section_encode' => 'ascii'
           ),
           array(
               'field' => '64',
               'use' => true,
               'len_section_type' => 'fix',
               'data_section_len' => 8,
               'data_section_encode' => 'bin'
           ),
       ); 
    }
}










